package org.tooyoung.fourone.map;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2016/8/31.
 */
public class NetworkTask<T> extends AsyncTask<NetworkTask.Callback<T>,Void,Object>{
    private static final String TAG = "NetworkTask";
    private Callback<T> callback;
    private Class<T> t;
    private String url;

    public NetworkTask(String url , Class<T> t) {
        this.t = t;
        this.url = url;
    }

    @Override
    protected Object doInBackground(Callback<T>... params) {
        callback = params[0];
        Log.d(TAG, "url: "+url);


        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            int code = connection.getResponseCode();


            if (code == 200) {
                InputStream is = connection.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int length;
                byte[] buffer = new byte[102400];
                while ((length = is.read(buffer)) != -1){
                    bos.write(buffer, 0, length);
                }
                String str = bos.toString("UTF-8");
                JSONObject jsonObject = new JSONObject(str);
                int status = jsonObject.getInt("status");
                String message = jsonObject.getString("message");
                Log.d(TAG, "doInBackground: status:"+status);
                Log.d(TAG, "doInBackground: message:"+message);

                if (message.equals("ok")) {
                    JSONArray result = jsonObject.getJSONArray("result");
                    JSONObject object = result.getJSONObject(0);
                    JSONObject location = object.getJSONObject("location");
                    Log.d(TAG, "doInBackground: "+location.toString());

                    Gson gson = new Gson();
                    return gson.fromJson(location.toString(), t);
                }else{
                    return new RuntimeException("ResponseCode: " + 666);
                }


            } else {
                  return new RuntimeException("ResponseCode: " + code);
            }
        } catch (IOException e) {
            return e;
        } catch (JSONException e) {
            return e;
        }
    }


    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        if (t.isInstance(o)) {
            callback.onSuccess((T)o);
        }else if(o instanceof Exception){
            callback.onFail((Exception)o);
        }

    }

    public interface Callback<S>{
        void onSuccess(S text);
        void onFail(Exception e);
    }
}
